import 'package:flutter/cupertino.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/bean/bin_info_entity.dart';
import 'package:xin_nuo_pda/bean/carrier_entity.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/http/api/lot_api.dart';
import 'package:xin_nuo_pda/routes/Routes.dart';

import '../../bean/arrtibut_balue_entity.dart';
import '../../bean/work_load_list_entity.dart';
import '../../values/constant.dart';
import 'PassEndWorkNextOneState.dart';

class PassEndWorkNextOneController extends BaseController {
  final PassEndWorkNextOneState state = PassEndWorkNextOneState();
  var defectList = <WorkLoadListEntity>[];
  String listID = 'listID';
  TextEditingController controller = TextEditingController();

  @override
  void onInit() {
    super.onInit();
    var map = Get.arguments;
    state.binBean = map["bean"];
    state.lotEntity = map["lotEntity"];

    WorkLoadListEntity bean = WorkLoadListEntity();
    bean.lotId = state.lotEntity?.lOTID ?? '';
    bean.userName = USER;
    bean.mainQty = state.binBean?.MAINQTY ?? 0;
    defectList.add(bean);
    update([listID]);
  }

  void addCarryData(String str) {
    state.carryList.add(str);
    update([state.carryLogicId]);
  }

  void deleteCarryData(String str) {
    state.carryList.remove(str);
    update([state.carryLogicId]);
  }

  addWork() {
    WorkLoadListEntity entity = WorkLoadListEntity();
    entity.lotId = state.lotEntity?.lOTID ?? '';
    defectList.add(entity);
    update([listID]);
  }

  trackOut() async {
    if(state.lotEntity?.REELPACKNUM == null || (state.lotEntity?.REELPACKNUM ?? '').isEmpty){
      Get.snackbar('提示', '请维护卷盘基数!');
      return;
    }

    double i = 0;

    defectList.forEach((element) {
      i += element.mainQty ?? 0;
    });

    if (i != (state.binBean?.MAINQTY ?? 0)) {
      Get.snackbar('提示', '工作总数量与出站数量不符!');
      return;
    }

    if (controller.text.isEmpty) {
      Get.snackbar('提示', '请输入机台数量!');
      return;
    }


    var carrierList = <CarrierEntity>[];
    state.carryList.forEach((element) {
      var carrier = CarrierEntity();
      carrier.CARRIERID = element;
      carrierList.add(carrier);
    });


    var binList = <BinInfo>[];
    state.binBean?.LOTID = state.lotEntity?.lOTID;
    binList.add(state.binBean ?? BinInfo());
    List<LotEntity> lotList = [];
    state.lotEntity?.mAINQTY = state.binBean?.MAINQTY;
    lotList.add(state.lotEntity ?? LotEntity());

    List<ArrtibutBalueEntity> arrList = [];
    ArrtibutBalueEntity arr = ArrtibutBalueEntity();
    arr.aTTRIBUTENAME = 'EquipmentNumber';
    arr.aTTRIBUTEVALUE = controller.text;
    arrList.add(arr);

    Map<String, dynamic> params = {
      'ACTIONTYPE': 'NomalTrackout',
      'LOTLIST': lotList,
      'LOTMULTICARRIERLIST': carrierList,
      'LOTBINDATALIST': binList,
      'WORKLOADLIST': defectList,
      'LOTATTRIBUTEVALUELIST' : arrList
    };
    var result = await LotApi.getSelectorBin(canelToken, params: params);
    if (result.ok) {
      Fluttertoast.showToast(msg: "出站成功");
      Get.offAllNamed(Routes.Tabs);
    }
  }
}
